using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using TeleTwitter.Lib;

namespace TeleTwitter
{
   static class VoiceTranslator
   {
      // WARNING - this is nothing but a bunch of hacks and should be refactored as soon as possible.

      //Process text output
      // - reverse order
      // - @username should translate to "Jason Alexander says to Scott Cate"
      // - pauses between tweets
      // - phrase URLs differently

       private static List<string> _spokenIds = new List<string>();

       public static string Translate(Timeline timeLine, List<Timeline> allTimeLines)
       {
           StringBuilder sb = new StringBuilder();

           for (int loopIndex = timeLine.StatusList.Count - 1; loopIndex > -1; loopIndex--)
           {
               Status loopStatus = timeLine.StatusList[loopIndex];

               if (_spokenIds.Contains(loopStatus.Id)) continue;

               sb.Append(" .. ");
               sb.AppendFormat("At {0}, {1} said .. ",
                   loopStatus.CreatedAt.ToString("hh:mm"), loopStatus.User.Name);
               sb.Append(ProcessText(loopStatus.Text, timeLine));

               _spokenIds.Add(loopStatus.Id);
               if (_spokenIds.Count > 100) _spokenIds.RemoveAt(0);
           }
 
           return sb.ToString();
       }

       private static string ProcessText(string inputText, Timeline timeLine)
       {
           inputText = Regex.Replace(inputText, @"([A-Z])([A-Z])", @" $1 $2 ");
           inputText = inputText.ToLower();

           // Replace all @<username> with " at <full name> " for every name we
           // currently know.
           foreach (Status loopStatus in timeLine.StatusList)
           {
               string screenName = loopStatus.User.ScreenName.ToLower();
               string fullName = loopStatus.User.Name.ToLower();

               inputText = inputText.Replace("@" + screenName + " ", " At " + fullName + " ");
               inputText = inputText.Replace("@" + screenName + ":", " At " + fullName + " ");
           }

           // Cleanup inputText using the Dictionary of abbreviations we've
           // configured it to know.
           foreach (DataRow loopRow in VoiceDictionary.Rows)
           {
               inputText = inputText.Replace(loopRow[0].ToString().ToLower(), loopRow[1].ToString().ToLower());
           }
           return inputText;
       }

       public static DataTable VoiceDictionary
       {
           get
           {
               DataTable vDict = Properties.Settings.Default.VoiceDictionary;
               if (vDict == null) vDict = CreateDefaultDictionary();
               return vDict;
           }
           set
           {
               Properties.Settings.Default.VoiceDictionary = value;
           }
       }

       private static DataTable CreateDefaultDictionary()
       {
           DataTable voiceDT = new DataTable();
           voiceDT.Columns.Add("Typed Text");
           voiceDT.Columns.Add("Spoken Text");

           voiceDT.Rows.Add("@", " @ ");
           voiceDT.Rows.Add("rofl", "rolling on the floor laughing");
           voiceDT.Rows.Add("http://", "");
           voiceDT.Rows.Add(" tt ", " tella twitter ");
           voiceDT.Rows.Add(" tt.", " tella twitter.");
           voiceDT.Rows.Add("teletwitter", "tella twitter");
           voiceDT.Rows.Add("teletwitter", "tella twitter");
           voiceDT.Rows.Add("tinyurl", "tie knee u r l");
           voiceDT.Rows.Add(" im ", " eye m ");
           voiceDT.Rows.Add(" im", " eye m.");
           voiceDT.Rows.Add(" xbox", " x box");
           voiceDT.Rows.Add(" imho", " in my humble opinion");
           voiceDT.Rows.Add(" lmao", " laughing my ass off");
           voiceDT.Rows.Add(" lmfao", " laughing my ass off");

           return voiceDT;
       }
//      public static string Translate(string content)
//      {
//         string output = "";
//         XmlDocument document = new XmlDocument();
//         document.LoadXml(content);
//         XmlElement root = document.DocumentElement;
//         XmlNodeList nodelist = root.SelectNodes("tweet");
//
//         foreach (XmlNode node in nodelist)
//         {
//            //do some cool stuff here
//            //string regex = @"<h2>(?<username>.*?)</h2>";
//            //string username = "";
//            //string directedto = "";
//            //if (Regex.IsMatch( node.InnerXml, regex ))
//            //{
//            //   username = Regex.Matches(node.InnerXml, regex)[0].Groups["username"].Value.ToString();
//            //}
//            node.InnerXml = node.InnerXml.ToLower();
//            string regex = "<span id=\"tlcreated\">(?<timeago>.*?)</span>";
//            if (Regex.IsMatch(node.InnerXml, regex))
//            {
//               string timeago = Regex.Matches(node.InnerXml, regex)[0].Groups["timeago"].Value.ToString();
//               node.InnerXml = node.InnerXml.Replace("<h2>", "<h2>" + timeago + " ");
//               node.InnerXml = node.InnerXml.Replace("<span id=\"tlcreated\">" + timeago + "</span>", "");
//            }
//            regex = "<div id=\"tltext\">@(?<directedto>.*?):.*?";
//            if (Regex.IsMatch(node.InnerXml, regex))
//            {
//               string directedto = Regex.Matches(node.InnerXml, regex)[0].Groups["directedto"].Value.ToString();
//               node.InnerXml = node.InnerXml.Replace("<div id=\"tltext\">@" + directedto, "<div id=\"tltext\"> to " + directedto + " ...");
//            }
//            node.InnerXml = node.InnerXml.Replace("</h2>", " said... </h2>");
//
//            // These all need to be moved to a config file ///////////////////////////////
//            node.InnerXml = node.InnerXml.Replace("rofl", "rolling on the floor laughing");
//            node.InnerXml = node.InnerXml.Replace("http://", "");
//            node.InnerXml = node.InnerXml.Replace(" tt ", " tella twitter ");
//            node.InnerXml = node.InnerXml.Replace(" tt.", " tella twitter.");
//            node.InnerXml = node.InnerXml.Replace("teletwitter", "tella twitter");
//            node.InnerXml = node.InnerXml.Replace("teletwitter", "tella twitter");
//            node.InnerXml = node.InnerXml.Replace("tinyurl", "tie knee u r l");
//            node.InnerXml = node.InnerXml.Replace(" im ", " eye m ");
//            node.InnerXml = node.InnerXml.Replace(" im", " eye m.");
//            node.InnerXml = node.InnerXml.Replace(" xbox", " x box");
//            node.InnerXml = node.InnerXml.Replace(" imho", " in my humble opinion");
//            node.InnerXml = node.InnerXml.Replace(" lmao", " laughing my ass off");
//            node.InnerXml = node.InnerXml.Replace(" lmfao", " laughing my ass off");
//            //////////////////////////////////////////////////////////////////////////////
//         }
//
//         output = ReverseOrderString(nodelist);
//         return output;
//      }

      //Reverse order of input and try to put in a pause between tweets. Also get rid of all the XML - it is not needed
      private static string ReverseOrderString(XmlNodeList nodelist)
      {
         string output = "";
         foreach (XmlNode node in nodelist) {
            output = node.InnerText + "... " + output;
         }
         return output;
      }
   }
}
